package com.dmg.sp.service.impl;

import com.dmg.sp.entity.MyUserDetails;
import com.dmg.sp.service.LoginService;
import com.dmg.sp.utils.JWTUtil;
import com.dmg.sp.utils.Result;
import com.dmg.sp.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {


    //认证管理器
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public Result login(LoginVo vo) {
        //根据账号和密码 创建 认证令牌对象
        UsernamePasswordAuthenticationToken upt=new UsernamePasswordAuthenticationToken(vo.getAccount(),vo.getPassword());
        //进行登录 获取认证信息
        Authentication authenticate = authenticationManager.authenticate(upt);
        if(authenticate==null){
            throw new RuntimeException("登录失败");
        }
        Map<String,String> map=new HashMap<>();
        map.put("account",vo.getAccount());
        //第二个参数是盐值
        //获取token
        String token= JWTUtil.getToken(map,"123456789");
        return Result.success(token);
    }
}
